﻿using JBeamLibrary.Tools;
using JBeamLibrary.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.VehicleParts.Interfaces;
using Newtonsoft.Json.Linq;

namespace JBeamLibrary.VehicleParts.Elements
{
    public class InternalCamera : VehicleElement
    {
        #region Constants



        #endregion

        #region Properties

        public Float3 Offset { get; set; }

        public float FOV { get; set; }

        public string NodeId1 { get; set; }

        public string NodeId2 { get; set; }

        public string NodeId3 { get; set; }

        public string NodeId4 { get; set; }

        public string NodeId5 { get; set; }

        public string NodeId6 { get; set; }

        public bool IsDefined
        {
            get
            {
                return
                    Offset.IsValid &&
                    FOV > float.MinValue &&
                    !string.IsNullOrEmpty(NodeId1) &&
                    !string.IsNullOrEmpty(NodeId2) &&
                    !string.IsNullOrEmpty(NodeId3) &&
                    !string.IsNullOrEmpty(NodeId4) &&
                    !string.IsNullOrEmpty(NodeId5) &&
                    !string.IsNullOrEmpty(NodeId6);
            }
        }

        #endregion

        #region CTORs

        public InternalCamera()
        {
            Offset = Float3.Invalid;
            FOV = float.MinValue;
        }

        public InternalCamera(Float3 offset, float fov, params string[] nodeIds)
        {
            Offset = offset;
            FOV = fov;
            for (var i = 0; i < nodeIds.Length; i++)
            {
                if (i == 0)
                    NodeId1 = nodeIds[i];
                else if (i == 1)
                    NodeId2 = nodeIds[i];
                else if (i == 2)
                    NodeId3 = nodeIds[i];
                else if (i == 3)
                    NodeId4 = nodeIds[i];
                else if (i == 4)
                    NodeId5 = nodeIds[i];
                else if (i == 5)
                    NodeId6 = nodeIds[i];
            }
        }

        #endregion

        #region Methods

        public override IVehicleElement FromJBeam(JToken token)
        {
            var arr = token as JArray;
            if (arr == null) return this;

            Offset = new Float3(
                token[0].FromJBeam<float>(),
                token[1].FromJBeam<float>(),
                token[2].FromJBeam<float>()
            );
            FOV = token[3].FromJBeam<float>();
            NodeId1 = token[4].FromJBeam<string>();
            NodeId2 = token[5].FromJBeam<string>();
            NodeId3 = token[6].FromJBeam<string>();
            NodeId4 = token[7].FromJBeam<string>();
            NodeId5 = token[8].FromJBeam<string>();
            NodeId6 = token[9].FromJBeam<string>();

            return this;
        }

        public override JToken ToJBeam()
        {
            return new JArray
            {
                Offset.X.ToJBeam(),
                Offset.Y.ToJBeam(),
                Offset.Z.ToJBeam(),
                FOV.ToJBeam(),
                NodeId1.ToJBeam(),
                NodeId2.ToJBeam(),
                NodeId3.ToJBeam(),
                NodeId4.ToJBeam(),
                NodeId5.ToJBeam(),
                NodeId6.ToJBeam()
            };
        }

        public void GetNodes(Vehicle vehicle, out Node node1, out Node node2, out Node node3, out Node node4, out Node node5, out Node node6)
        {
            node1 = vehicle.FindNode(NodeId1);
            node2 = vehicle.FindNode(NodeId2);
            node3 = vehicle.FindNode(NodeId3);
            node4 = vehicle.FindNode(NodeId4);
            node5 = vehicle.FindNode(NodeId5);
            node6 = vehicle.FindNode(NodeId6);
        }

        public override string ToString()
        {
            return string.Format("Offset: {0}; FOV: {1}", Offset, FOV);
        }

        #endregion
    }
}
